// pages/ArticlePage/ArticlePage.js
import Toast from '@vant/weapp/toast/toast';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        title: '',
        content: '内容加载中...',
        createdTime: '',
    },
    downloadFile(url) {
        // debugger
        let _this = this
        Toast.loading({
            message: '文章加载中...',
            duration: 0,
            forbidClick: true,
            loadingType: 'spinner',
        });
        // debugger
        wx.downloadFile({
          url: url,
          success: function(res) {
            var filePath = res.tempFilePath;
            console.log(filePath);
            wx.openDocument({
              filePath: filePath,
              fileType: 'pdf',
              success: function(res) {
                  console.log(res);
                  Toast.clear();
                  wx.reLaunch({
                    url: '/pages/RelevantData/RelevantData'
                  })                  
              },
              fail: function(res) {
                  console.log(res);
                  Toast.clear();
              },
              complete: function(res) {
                  console.log(res);
              }
            })
          },
          fail: function(res) {
            Toast.clear();   
              console.log('文件下载失败');
          },
          complete: function(res) {
            Toast.clear();
          },
        })
      },
    getArticle(id) {
        let _this = this
        wx.request({
            url: 'https://gongxiangapi.haonanzhang.cn/api/electric/articleDetail',
            method: 'get',
            data: {
                articleId: id
            },
            contentType: 'application/json',
            success({
                data: res
            }) {
                // let list = data.data.list
                // debugger
                //图片自适应
                // let wxWidth = wx.getSystemInfoSync().windowWidth
                // debugger
                _this.setData({
                    title: res.data.title || '',
                    createdTime: res.data.createdTime || '',
                })
                if(res.data.content.indexOf(".pdf") > -1){
                    // debugger
                    _this.downloadFile(res.data.content)
                    return;
                }
                let content1 = _this.formatRichText(res.data.content)
                // console.log(content1)
                // debugger
                _this.setData({
                    title: res.data.title || '',
                    content: content1 || '暂无内容',
                    createdTime: res.data.createdTime || '',
                })
                Toast.clear();
                //  let { data } = data
                //  debugger
            },
            fail(){
                Toast.clear();
            },
            complete(){
                // Toast.clear();
            }
        })
    },
    formatRichText(html){
        let newContent= html.replace(/<img[^>]*>/gi,function(match,capture){
          match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
          match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
          match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
          return match;
        });
        newContent = newContent.replace(/style="[^"]+"/gi,function(match,capture){
          match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
          return match;
        });
        newContent = newContent.replace(/<br[^>]*\/>/gi, '');
        // newContent = newContent.replace(/&nbsp;|&ldquo;|&rdquo;/ig, "");
        newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;margin-top:0;margin-bottom:0;"');
        //设置字体大小
        newContent = newContent.replace(/font-size:[^;]+;/gi, 'font-size: 14px;')
        return newContent;
      },
    copy: function (e) {
        var that = this;
        var text = e.currentTarget.dataset.text;
        var msg = text.replace(/<(\/)?[^>].*?>/g, '');
        msg = msg.replace(/&nbsp;|&ldquo;|&rdquo;/ig, "");
        wx.setClipboardData({
            data: msg,
            success: function (res) {
                // wx.hideToast();    //打开可不显示提示框
                wx.getClipboardData({
                    success(res) {
                        console.log(res.data) // data
                    }
                })
            }
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (this.options.articleId) {
            // 自定义加载图标
            // 自定义加载图标
            Toast.loading({
                message: '加载中...',
                duration: 0,
                forbidClick: true,
                loadingType: 'spinner',
            });
            
            this.getArticle(this.options.articleId)
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        // debugger
        

        // Toast.loading({
        //     message: '加载中...',
        //     forbidClick: true,
        //     loadingType: 'spinner',
        // });
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})